import * as service from "@/service/index";
import { IHome } from "./interface";

const home: IHome = {
  state: {
    headerList: [],
    bannerList: [],
    navList: [],
    navSwipeList: [],
    ddqList: [],
    ddqActive: "",
    brandList: [],
    groupList: [],
    footerList: [],
    footerList2: [],
    sliderList: [],
  },
  actions: {
    async getHeaderList({ commit }) {
      const res = await service.fetchHomeHeader();
      commit({
        type: "GET_HEADER_LIST",
        payload: res?.data,
      });
    },
    async getSliderList({ commit }, { data }) {
      const res = await service.fetchHomeProduct(data);
      commit({
        type: "GET_SLIDER_LIST",
        payload: res?.data?.config,
      });
    },
    async getBannerList({ commit }, { data }) {
      const res = await service.fetchHomeHome(data);
      commit({
        type: "GET_BANNER_LIST",
        payload: res?.data?.config?.oneBanners,
      });
    },
    async getNavList({ commit }) {
      const res = await service.fetchHomeNav();
      commit({
        type: "GET_NAV_LIST",
        payload: res?.data?.config?.data,
      });
    },
    async getNavSwipeList({ commit }, { data }) {
      const res = await service.fetchHomeProduct(data);
      commit({
        type: "GET_NAV_SWIPE_LIST",
        payload: res?.data?.config?.list,
      });
    },
    async getDdqList({ commit }, { data }) {
      const res = await service.fetchHomeProduct(data);
      commit({
        type: "GET_DDQ_LIST",
        payload: res?.data?.ddqSessions,
      });
    },
    async getBrandList({ commit }, { data }) {
      const res = await service.fetchHomeProduct(data);
      commit({
        type: "GET_BRAND_LIST",
        payload: res?.data,
      });
    },
    async getGroupList({ commit }, { data }) {
      const res = await service.fetchHomeProduct(data);
      commit({
        type: "GET_GROUP_LIST",
        payload: res?.data?.config,
      });
    },
    async getFindList({ commit }, { data }) {
      const res = await service.fetchHomeFooterFind(data);
      commit({
        type: "GET_FIND_LIST",
        payload: res?.data?.list,
      });
    },
    async getChooseList({ commit }) {
      const res = await service.fetchHomeChooseList();
      commit({
        type: "GET_CHOOSE_LIST",
        payload: res?.data,
      });
    },
  },
  mutations: {
    GET_HEADER_LIST(state, { payload }) {
      state.headerList = payload;
    },
    GET_SLIDER_LIST(state, { payload }) {
      state.sliderList = payload.map((item) => {
        return item.pic;
      });
    },
    GET_BANNER_LIST(state, { payload }) {
      state.bannerList = payload.map((item) => {
        return item.bannerImgUrl;
      });
    },
    GET_NAV_LIST(state, { payload }) {
      state.navList = payload;
    },
    GET_NAV_SWIPE_LIST(state, { payload }) {
      state.navSwipeList = payload;
    },
    GET_DDQ_LIST(state, { payload }) {
      state.ddqList = payload;
      state.ddqActive = payload.find((item: any) => item.status == 0)?.timeDesc;
    },
    GET_BRAND_LIST(state, { payload }) {
      state.brandList = payload;
    },
    GET_GROUP_LIST(state, { payload }) {
      state.groupList = payload;
    },
    GET_FIND_LIST(state, { payload }) {
      state.footerList.push(...payload);
    },
    GET_CHOOSE_LIST(state, { payload }) {
      state.footerList2.push(...payload);
    },
  },
  namespaced: true,
};
export default home;
